package com.jz.community.modulemine.integral.ui;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseHtml5Activity;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.constant.HtmlConstant;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.commview.view.EmptyInfoLayout;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.integral.adapter.IntegralAdapter;
import com.jz.community.modulemine.integral.bean.IntegralInfo;
import com.jz.community.modulemine.integral.task.GetIntegralTask;
import com.oushangfeng.pinnedsectionitemdecoration.PinnedHeaderItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/6/14 17:47
 * 我的积分
 */
@Route(path = RouterIntentConstant.MY_INTEGRAL)
public class MyIntegralActivity extends BaseMvpActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.money_back)
    ImageView moneyBack;
    @BindView(R2.id.money_title)
    TextView moneyTitle;
    @BindView(R2.id.money_detail)
    TextView moneyDetail;
    @BindView(R2.id.text_number_shequmoney)
    TextView textNumberShequmoney;
    @BindView(R2.id.money_rv)
    RecyclerView moneyRv;
    @BindView(R2.id.money_refresh)
    SmartRefreshLayout moneyRefresh;
    @BindView(R2.id.money_toolbar)
    Toolbar moneyToolbar;
    @BindView(R2.id.my_integral_layout)
    LinearLayout myIntegralLayout;
    @BindView(R2.id.myIntegral_empty_layout)
    EmptyInfoLayout myIntegralEmptyLayout;
    private int page = 0;
    IntegralAdapter integralAdapter;

    @Override
    protected int getContentView() {
        return R.layout.module_mine_activity_money;
    }


    @Override
    protected void initView() {
        moneyTitle.setText("我的积分");
        setImmersionBar(moneyToolbar);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        handleIntegralAdapter();
        getIntegralDatas(true);
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    protected void addListener() {
        moneyBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        moneyDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyIntegralActivity.this, BaseX5WebActivity.class);
                intent.putExtra("url", HtmlConstant.INTEGRAL_HTML_URL);
                intent.putExtra("title", "积分规则");
                startActivity(intent);
            }
        });
    }

    private void handleIntegralAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        moneyRefresh.setOnRefreshListener(this);//刷新监听
        moneyRefresh.setEnableLoadMore(false);
        moneyRv.setLayoutManager(layoutManager);
        moneyRv.addItemDecoration(new PinnedHeaderItemDecoration.Builder(0x00000444).enableDivider(false).create());
        integralAdapter = new IntegralAdapter(R.layout.module_mine_item_shequmoney, new ArrayList<>());
        integralAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);//加载动画
        integralAdapter.setOnLoadMoreListener(this, moneyRv);
        moneyRv.setAdapter(integralAdapter);
    }

    private void getIntegralDatas(boolean isRefresh) {
        new GetIntegralTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                IntegralInfo integralInfo = (IntegralInfo) o;
                if (!Preconditions.isNullOrEmpty(integralInfo)) {
                    moneyRefresh.finishRefresh();
                    loadIntegralInfo(integralInfo, isRefresh);
                }
            }
        }).execute(page + "");
    }

    private void loadIntegralInfo(IntegralInfo integralInfo, boolean isRefresh) {
        if (!Preconditions.isNullOrEmpty(integralInfo.get_embedded()) && !Preconditions.isNullOrEmpty(integralInfo.get_embedded().getContent())) {
            SHelper.gone(myIntegralEmptyLayout);
            SHelper.vis(myIntegralLayout);
            textNumberShequmoney.setText(integralInfo.get_embedded().getContent().get(0).getResidualIntegral() + "");
            setData(isRefresh, integralInfo.get_embedded().getContent(), integralInfo.getPage().getTotalPages());
        } else {
            myIntegralEmptyLayout.setEmptyTxt("暂无积分记录！");
            SHelper.vis(myIntegralEmptyLayout);
        }
    }

    private void setData(boolean isRefresh, List data, int totalPages) {

        page++;

        if (isRefresh) {
            integralAdapter.setNewData(data);
        } else {
            integralAdapter.addData(data);
        }

        if (totalPages <= page) {
            integralAdapter.loadMoreEnd();
        } else {
            integralAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        integralAdapter.setEnableLoadMore(false);
        page = 0;
        getIntegralDatas(true);
    }

    @Override
    public void onLoadMoreRequested() {
        getIntegralDatas(false);
    }
}
